package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsTaskCategoryTypeEnum;
import qc.module.ehs.dto.taskexecute.TaskExecuteAddDto;
import qc.module.ehs.dto.taskexecute.TaskExecuteInfoDto;
import qc.module.ehs.dto.taskexecute.TaskExecuteUpdateDto;
import qc.module.ehs.entity.EhsTaskExec;

import java.util.List;

/**
 * EhsTaskExecMapper
 * 
 * @author QuCheng Tech
 * @since 2024/11/13
 */
@Mapper
public interface EhsTaskExecMapper {

    EhsTaskExecMapper MAPPER = Mappers.getMapper(EhsTaskExecMapper.class);

    @Mapping(target = "begintm", source = "begintm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "endtm", source = "endtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "confirmtm", source = "confirmtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "tasktype", source = "tasktype.index")
    TaskExecuteInfoDto toDto(EhsTaskExec en);
    
    List<TaskExecuteInfoDto> toDtoList(List<EhsTaskExec> ens);

    @Mapping(target = "begintm", source = "begintm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "endtm", source = "endtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "tasktype", ignore = true)
    EhsTaskExec toEntity(TaskExecuteAddDto addDto);

    default EhsTaskExec addDtoToEntity(TaskExecuteAddDto dto) {
        EhsTaskExec en = EhsTaskExecMapper.MAPPER.toEntity(dto);

        en.setTasktype(getEhsTaskCategoryTypeEnum(dto.getTasktype()));

        return en;
    }

    @Mapping(target = "begintm", source = "begintm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "endtm", source = "endtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "tasktype", ignore = true)
    EhsTaskExec toEntity(TaskExecuteUpdateDto updateDto);

    static EhsTaskCategoryTypeEnum getEhsTaskCategoryTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsTaskCategoryTypeEnum item : EhsTaskCategoryTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为1--自查类任务
        return EhsTaskCategoryTypeEnum.SECURITY_CHECK;
    }
}
